//
//  MineViewController.m
//  TimeBoat
//
//  Created by yang on 2022/7/5.
//  Copyright © 2022 yang. All rights reserved.
//

#import "UserInfoViewController.h"
#import "BRStringPickerView.h"
#import "BRDatePickerView.h"
#import "PPTextfield.h"
#import "LoginUtils.h"

@interface UserInfoViewController ()

@property (weak, nonatomic) IBOutlet UIImageView *img;
@property (weak, nonatomic) IBOutlet PPTextfield *nickNameTextfield;
@property (weak, nonatomic) IBOutlet UILabel *sexLab;
@property (weak, nonatomic) IBOutlet UILabel *birthdayLab;
@property (weak, nonatomic) IBOutlet PPTextfield *heightTextField;
@property (weak, nonatomic) IBOutlet PPTextfield *weightTextField;
@property (weak, nonatomic) IBOutlet PPTextfield *phoneTextfield;

@end


@implementation UserInfoViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    self.hidesBottomBarWhenPushed = YES;
    self.title = @"帐号与资料";
    [self initRightNavBtnWithText:@"完成"];
    self.heightTextField.isPrice = YES;
    self.heightTextField.pointNum = 2;
    self.heightTextField.maxAmount = 999.99;
    
    self.weightTextField.isPrice = YES;
    self.weightTextField.pointNum = 2;
    self.weightTextField.maxAmount = 999.99;
    
    [self setUserInfo];
}


- (void)setUserInfo{
    NSString *nicknameStr = [AccountManager shareAccount].loginModel.nickname;
    if (nicknameStr.length == 0) {
        NSString *newTelephone = [self.telephone stringByReplacingCharactersInRange:NSMakeRange(3, 4) withString:@"****"];
        nicknameStr = [NSString stringWithFormat:@"%@",newTelephone];
    }
    self.nickNameTextfield.text = [nicknameStr isEqualToString:@"(null)"] ? @"" :nicknameStr;
    
    NSString *avatarUrl = [AccountManager shareAccount].loginModel.avatarUrl;
    
    NSString *gender = [AccountManager shareAccount].loginModel.gender;
    if([gender isEqualToString:@"2"]){
        [self.img sd_setImageWithURL:[NSURL URLWithString:avatarUrl] placeholderImage:[UIImage imageNamed:@"information_icon_girl"]];
        gender = @"女";
    }
    else {
        [self.img sd_setImageWithURL:[NSURL URLWithString:avatarUrl] placeholderImage:[UIImage imageNamed:@"information_icon_boy"]];
        gender = @"男";
    }
    
    self.sexLab.text = gender;
    
    self.birthdayLab.text = [AccountManager shareAccount].loginModel.birthday;
    
    NSString *height = [AccountManager shareAccount].loginModel.height;
    if (height.length == 0) {
        height = @"170";
    }
    self.heightTextField.text = height;
    
    NSString *weight = [AccountManager shareAccount].loginModel.weight;
    if (weight.length == 0) {
        weight = @"60";
    }
    self.weightTextField.text = weight;
    
    self.phoneTextfield.text = [AccountManager shareAccount].loginModel.phone;
}

#pragma mark - 修改资料
- (void)topMoreClick{
    //gender性别,2-女/1-男
    NSString *gender = self.sexLab.text;
    if ([gender isEqualToString:@"男"]) {
        gender = @"1";
    }else if([gender isEqualToString:@"女"]){
        gender = @"2";
    }
    
    NSString *tokenStr = [AccountManager shareAccount].loginModel.token;
    NSString *avatarUrl = [AccountManager shareAccount].loginModel.avatarUrl;
    
    NSMutableDictionary *parameter = @{
        @"avatarUrl": avatarUrl,
        @"birthday": self.birthdayLab.text,
        @"gender": gender,
        @"height": self.heightTextField.text,
        @"nickname": self.nickNameTextfield.text,
        @"token": tokenStr == nil ? @"" : tokenStr,
        @"weight": self.weightTextField.text
    }.mutableCopy;
    
    [[HttpHelp shareInstance] POSTHTTPV2:API_LOGIN_updateInfo parameters:parameter success:^(NSDictionary * _Nonnull dict) {
        NSInteger result = [dict[@"result"] integerValue];
        if (result == 1) {
            [self mbShowToast:@"修改成功"];
            NSDictionary *tempDict = dict[@"data"];
            LoginModel *tempModel = [LoginModel mj_objectWithKeyValues:tempDict];
            [[AccountManager shareAccount] update:tempModel];
            [self.navigationController popToRootViewControllerAnimated:YES];
        }
    } failure:^(BOOL netErr, NSString * _Nonnull error) {
        DMLog(@"%@", error);
    }];
}

- (IBAction)logOutClick:(UIButton *)sender {
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@""
                                                                   message:@"确定退出登录?"
                                                            preferredStyle: UIAlertControllerStyleActionSheet];
    
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消"
                                                           style:UIAlertActionStyleCancel
                                                         handler:^(UIAlertAction * action) {
        
    }];
    
    UIAlertAction *deleteAction = [UIAlertAction actionWithTitle:@"退出"
                                                           style:UIAlertActionStyleDestructive
                                                         handler:^(UIAlertAction * action) {
        [[AccountManager shareAccount] logout];
        [self.navigationController popToRootViewControllerAnimated:YES];
    }];
    
    [alert addAction:cancelAction];
    [alert addAction:deleteAction];
    
    [self presentViewController:alert animated:YES completion:nil];
}


#pragma mark - 点击性别
- (IBAction)sexClick:(UIButton *)sender {
    [self.view endEditing:YES];
    
    WeakSelf(self)
    BRStringPickerView *pickerView = [[BRStringPickerView alloc]init];
    pickerView.pickerMode = BRStringPickerComponentSingle;
    pickerView.title = @"";
    pickerView.dataSourceArr = @[@"男", @"女"];
    pickerView.resultModelBlock = ^(BRResultModel *resultModel) {
        StrongSelf(weakSelf)
        strongSelf.sexLab.text = resultModel.value;
    };
    
    [pickerView show];
}


#pragma mark - 点击生日
- (IBAction)birthdayClick:(UIButton *)sender {
    [self.view endEditing:YES];
    
    BRDatePickerView *pickerView = [[BRDatePickerView alloc]init];
    pickerView.pickerMode = BRDatePickerModeYMD;
    pickerView.selectValue = self.birthdayLab.text;
    __weak typeof(self) wself = self;
    pickerView.resultBlock = ^(NSDate *selectDate, NSString *selectValue) {
        wself.birthdayLab.text = selectValue;
    };
    
    [pickerView show];
}


@end
